package com.lazycat.backend.service.impl.chat;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lazycat.backend.mapper.PersonalMessagesMapper;
import com.lazycat.backend.mapper.UserMapper;
import com.lazycat.backend.pojo.PersonalMessages;
import com.lazycat.backend.pojo.User;
import com.lazycat.backend.service.chat.GetMessageSingleEndService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class GetMessageSingleEndServiceImpl implements GetMessageSingleEndService {
    @Autowired
    private PersonalMessagesMapper personalMessagesMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public HashMap<String, Object> get(Integer senderId) {
        HashSet<Integer> ids = new HashSet<>();
        ArrayList<JSONObject> items = new ArrayList<>();
        HashMap<String, Object> resp = new HashMap<>();
        QueryWrapper<PersonalMessages> personalMessageQueryWrapper = new QueryWrapper<>();
        personalMessageQueryWrapper.eq("sender_id", senderId).orderByDesc("time");
        List<PersonalMessages> personalMessages = personalMessagesMapper.selectList(personalMessageQueryWrapper);
        QueryWrapper<PersonalMessages> _personalMessageQueryWrapper = new QueryWrapper<>();
        _personalMessageQueryWrapper.eq("receiver_id", senderId).orderByDesc("time");
        personalMessages.addAll(personalMessagesMapper.selectList(_personalMessageQueryWrapper));
        personalMessages.sort(new Comparator<PersonalMessages>() {
            @Override
            public int compare(PersonalMessages o1, PersonalMessages o2) {
                return o1.getTime().compareTo(o2.getTime());
            }
        });
        if (personalMessages == null) {
            resp.put("error_message", "no message");
            return resp;
        }
        for (int i = personalMessages.size() - 1; i >= 0; i--) {
            PersonalMessages personalMessage = personalMessages.get(i);
            JSONObject item = new JSONObject();
            Integer rid = personalMessage.getSenderId() + personalMessage.getReceiverId()
                            - senderId; // math!
            // System.out.println(personalMessage.getSenderId() + " " + personalMessage.getReceiverId());
            String message = personalMessage.getMessage();

            if (!ids.contains(rid)) {
                item.put("another_user_id", rid);
                item.put("last_message", message);
                item.put("last_message_time", personalMessage.getTime());
                ids.add(rid);

                QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
                userQueryWrapper.eq("id", rid);
                User user = userMapper.selectOne(userQueryWrapper);
                item.put("another_user_name", user.getUsername());
                items.add(item);
            }
        }
        resp.put("error_message", "success");
        resp.put("messages", items);
        return resp;
    }
}
